package com.zxy.service.v1;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zxy.dao.v1.UserDao;
import com.zxy.entity.v1.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @Description: TODO
 * @Author: zhuxianyu
 * @Date: 2021/12/17 21:19
 * @Version: 1.0
 */
@Service
public class UserService {

    @Autowired
    UserDao userDao;

    public void httpRequest(String status, int n) {
        //调用的api的接口地址
        String apiPath = "http://127.0.0.1:8080/v1/select/person?status=" + status + "&n=" + n;
        BufferedReader in = null;
        StringBuffer result;
        try {
            URL url = new URL(apiPath);
            //打开和url之间的连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setRequestProperty("Charset", "utf-8");
            connection.connect();
            result = new StringBuffer();
            //读取URL的响应
            in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
            String result2 = result.toString(); //返回json字符串
            //获取数据
            JSONObject jsonObject = JSON.parseObject(result2);

//            String code = jsonObject.getString("code");
//            System.out.println(code);
//
//            JSONObject resultJsonObject = jsonObject.getJSONObject("result");
//            JSONObject bodyJsonObject = resultJsonObject.getJSONObject("showapi_res_body");
//            JSONArray jsonArray = bodyJsonObject.getJSONArray("contentlist");

            JSONArray jsonArray = jsonObject.getJSONArray("personlist");

            //遍历json集合，取出数据
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject2 = (JSONObject) jsonArray.get(i);
                //System.out.println(jsonObject2);
                User user = new User();
                //jsonObject2.get("x_x").toString()中的“x_x”要和实际返回的json数据中的字段名一致，否则可能会出现找不到字段的错误
                user.setUuid(jsonObject2.get("uuid").toString());
                user.setDistinct_id(jsonObject2.get("distinct_id").toString());
                user.setMobile(jsonObject2.get("mobile").toString());
                user.setEmail(jsonObject2.get("email").toString());
                user.setNick_name(jsonObject2.get("nick_name").toString());

                //dao层保存数据存入数据库
                userDao.save(user);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

}
